package com.bristua.barcode.order.biz;

import com.bristua.barcode.common.bean.CustomerParams;
import com.bristua.barcode.common.constants.Constants;
import com.bristua.barcode.common.enums.CommonCode;
import com.bristua.barcode.common.exception.BizException;
import com.bristua.barcode.common.util.*;
import com.bristua.barcode.customer.dao.TCustomerMapper;
import com.bristua.barcode.customer.enums.CustomerCode;
import com.bristua.barcode.customer.model.TCustomer;
import com.bristua.barcode.customer.model.bo.CustomerBindQRCode;
import com.bristua.barcode.customer.model.condition.QueryOrderParams;
import com.bristua.barcode.customer.model.condition.SendOrderParams;
import com.bristua.barcode.customer.model.condition.TCustomerExample;
import com.bristua.barcode.order.dao.TPayOrderMapper;
import com.bristua.barcode.order.enums.OrderCode;
import com.bristua.barcode.order.model.TPayOrder;
import com.bristua.barcode.order.model.condition.TPayOrderExample;
import com.bristua.barcode.order.model.vo.QueryCustomerOrderVO;
import com.bristua.barcode.order.model.vo.SendOrderSuccessVO;
import com.bristua.barcode.supplier.dao.TQrCodeMapper;
import com.bristua.barcode.supplier.model.TQrCode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;


/**
 * Created by Administrator on 2019/8/1 0001.
 */
@Service
@Transactional
public class QueryOrderBiz {

    private static final Logger logger = LoggerFactory.getLogger(QueryOrderBiz.class);

    @Autowired
    private HttpSessionUtil httpSessionUtil;


    @Resource
    private TPayOrderMapper tPayOrderMapper;



    public QueryCustomerOrderVO queryOrder(QueryOrderParams queryOrderParams) {
        CustomerParams userInfo = httpSessionUtil.getUserInfo();
        String sign = queryOrderParams.getSign();
        StringBuilder sb = new StringBuilder();
        sb.append(userInfo.getAppId()).append(queryOrderParams.getOrderNo())
                .append(queryOrderParams.getTimestap()).append(userInfo.getKeyStore());
        String signStr = MD5Util.Bit32(sb.toString());

        if (!signStr.equals(queryOrderParams.getSign())) {
            logger.error("==>签名错误,客户端签名:"+queryOrderParams.getSign()+",服务端签名:"+signStr);
            throw new BizException(CommonCode.SING_FAIL.getCode(), CommonCode.SING_FAIL.getMsg());
        }
        TPayOrderExample tPayOrderExample = new TPayOrderExample();
        tPayOrderExample.createCriteria().andCustomerOrderNoEqualTo(queryOrderParams.getOrderNo());
        List<TPayOrder> tPayOrders = tPayOrderMapper.selectByExample(tPayOrderExample);
        TPayOrder tPayOrder = tPayOrders.get(0);
        QueryCustomerOrderVO queryCustomerOrderVO = new QueryCustomerOrderVO();
        BeanUtils.copyProperties(tPayOrder,queryCustomerOrderVO);
        return queryCustomerOrderVO;
    }


}
